<template>
  <div>
    <el-form :model="currentRecord" ref="editForm" label-width="80px" :rules="formRules">
      <el-form-item label="上级部门" prop="parentId">
        <el-cascader
          v-model="currentRecord.parentId"
          :options="departmentTree"
          :show-all-levels="false"
          :disabled="this.mode == 'details'"
          clearable
          :props="{ label: 'name', value: 'id', checkStrictly: true }"
        ></el-cascader>
      </el-form-item>

      <el-form-item label="节点名称" prop="name">
        <el-input v-model="currentRecord.name" :disabled="this.mode == 'details'" placeholder="请输入节点名称"></el-input>
      </el-form-item>
      <el-form-item label="排序" prop="orderId">
        <el-input-number v-model="currentRecord.orderId"  :min="1" :max="10" label="请输入排序"></el-input-number>

      </el-form-item>
      <el-form-item align="center">
        <el-col :span="24">
          <el-button type="primary" :loading="editLoading" @click.native.prevent="handleSubmit">
            <i class="fa fa-save"></i> 保存
          </el-button>
        </el-col>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { Message } from 'element-ui'
import DepartmentApi from '../api/department'

export default {
  name: 'DepartmentEdit',
  props: {
    mode: {
      type: String
    },
    record: {
      type: Object
    },
    departmentTree: {
      type: Array,
      default: () => [],
    },
  },
  data() {
    return {
      currentRecord: {},
      formRules: {
        name: [{ required: true, message: '请输入节点名称', trigger: 'blur' }],
        orderId: [{ required: true, message: '请输入排序', trigger: 'blur' }]
      },
      editLoading: false
    }
  },
  watch: {
    record: {
      handler (oldValue, newValue) {
      this.currentRecord = this.record
    },
    immediate:true,
    deep:true
    }
  },
  methods: {

    handleSubmit() {
      this.$refs['editForm'].validate(valid => {
        if (valid) {
          this.currentRecord.parentId = Array.isArray(
            this.currentRecord.parentId
          )
            ? this.currentRecord.parentId.length
              ? this.currentRecord.parentId.at(-1)
              : null
            : this.currentRecord.parentId;
          let newRecord = Object.assign({}, this.currentRecord)
          if (this.mode === 'add') {
            this.editLoading = true
            DepartmentApi.add(newRecord).then(rspData => {
              if (rspData.data.code === 200) {
                Message.success('新增成功')
              } else {
                Message.error(rspData.data.message)
              }
              this.editLoading = false
              this.$emit('after-edit')
            })
          } else if (this.mode === 'details') {
            this.$emit('after-edit')
          } else {
            this.editLoading = true
            DepartmentApi.update(newRecord).then(rspData => {
              if (rspData.data.code === 200) {
                Message.success('修改成功')
              } else {
                Message.error(rspData.data.message)
              }
              this.editLoading = false
              this.$emit('after-edit')
            })
          }
        } else {
          return false
        }
      })
    },
    resetFields() {
      this.$refs['editForm'].resetFields()
    }
  }
}
</script>
